<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>密码找回</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.png">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/login.css" media="all">
</head>
<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>Admin</h2>
            <p>信息科学与技术学院</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">

            <script type="text/html" template>
                {{# if(layui.router().search.type === 'resetpass'){ }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-password"></label>
                    <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass"
                           placeholder="新密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-repass"></label>
                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required"
                           placeholder="确认密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-resetpass">重置新密码
                    </button>
                </div>
                {{# } else { }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-cellphone"
                           for="LAY-user-login-cellphone"></label>
                    <input type="text" name="cellphone" id="LAY-user-login-cellphone" lay-verify="phone"
                           placeholder="请输入注册时的手机号" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                   for="LAY-user-login-vercode"></label>
                            <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required"
                                   placeholder="图形验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;" id="checkCaptcha">
                                <img src="${pageContext.request.contextPath}/captcha" width="130px" height="38px"
                                     title="刷新二维码" onclick="getCaptcha()"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                   for="LAY-user-login-smscode"></label>
                            <input type="text" name="vercode" id="LAY-user-login-smscode" lay-verify="required"
                                   placeholder="短信验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid"
                                        onclick="getPhoneVercode()">获取验证码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-submit">找回密码
                    </button>
                </div>
                {{# } }}
            </script>

        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router();

        form.render();
        //刷新验证码
        window.getCaptcha = function getCaptcha() {
            $.ajax({
                url: "${pageContext.request.contextPath}/captcha",
                type: "GET",
                success: function (data) {
                    $("#checkCaptcha").html(' <img src="${pageContext.request.contextPath}/captcha" width="130px" height="38px" title="刷新二维码" onclick="getCaptcha()"/>');
                }
            })
        }

        //获得短信验证码
        window.getPhoneVercode = function getPhoneVercode() {
            let phone = $("#LAY-user-login-cellphone")
            phone.data = phone.val()
            //验证是否符合电话号码的规范
            if (/^1([3456789])\d{9}$/.test(phone.data)) {
                $.post({
                    url: '${pageContext.request.contextPath}/login?method=getVercode&id=1',
                    data: {
                        data: JSON.stringify(phone)
                    },
                    success: function (data) {
                        layer.confirm(JSON.parse(data).msg, {icon: 1, title: '提示'}, function (index) {
                            layer.close(index);
                        });
                    }
                })
            } else {
                layer.msg('请重新输入正确的电话号码')
            }
        }

        //找回密码下一步
        form.on('submit(LAY-user-forget-submit)', function (obj) {
            var field = obj.field;
            let vercode = $("#LAY-user-login-smscode")
            vercode.data = vercode.val()
            let phone = $("#LAY-user-login-cellphone")
            phone.data = phone.val()
            let captcha = $("#LAY-user-login-vercode")
            captcha.data = captcha.val()
            //判断验证码是不是对的
            let flag = false;
            //检验验证码
            $.post({
                //异步属性
                async: false,
                url: '${pageContext.request.contextPath}/login?method=checkVercode',
                data: {
                    vercode: JSON.stringify(vercode),
                    phone: JSON.stringify(phone)
                },
                success: function (data) {
                    if (JSON.parse(data).msg == 1) {
                        flag = true;
                    }
                }
            })
            if (flag === false) {
                return layer.msg('请输入正确的短信验证码');
            }
            //验证图形验证码
            let captchaflag = false;
            $.post({
                //异步属性
                async: false,
                url: '${pageContext.request.contextPath}/login?method=checkCaptcha',
                data: {
                    captcha: JSON.stringify(captcha),
                },
                success: function (data) {
                    if (JSON.parse(data).msg == 1) {
                        captchaflag = true;
                    }
                }
            })
            if (captchaflag === false) {
                return layer.msg('请输入正确的图形验证码');
            }
            //请求接口
            admin.req({
                url: '${pageContext.request.contextPath}/login?method=update1' //实际使用请改成服务端真实接口
                , data: {
                    data: JSON.stringify(field)
                }
                , done: function (res) {
                    if (res.msg == 1) {
                        layer.msg('没有这个用户', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        });
                    } else if (res.msg == 2) {
                        layer.msg('验证码错误', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        });
                    } else {
                        location.hash = '/type=resetpass';
                        location.reload();
                    }
                }
            });
            return false;
        });

        //重置密码
        form.on('submit(LAY-user-forget-resetpass)', function (obj) {
            let field = obj.field;

            //确认密码
            if (field.password !== field.repass) {
                return layer.msg('两次密码输入不一致');
            }

            //请求接口
            admin.req({
                url: '${pageContext.request.contextPath}/login?method=update2' //实际使用请改成服务端真实接口
                , data: {
                    data: JSON.stringify(obj.field)
                }
                , done: function (res) {
                    if (res.msg == 0) {
                        layer.msg('密码已成功重置', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.href = '${pageContext.request.contextPath}/admin/login.jsp'
                        });
                    } else {
                        layer.msg('密码重置失败', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        }, function () {
                            location.href = '${pageContext.request.contextPath}/admin/login.jsp'
                        });
                    }
                }
            });

            return false;
        });

    });
</script>
</body>
</html>
